//自定义注册业务操作对象
window.retrieve = (function () {
    var obj = {};
    obj.steps = 3;//总流程步骤
    obj.now = 1;//当前步骤
    //操作界面变化
    obj.change = function () {
        if (obj.now > 0 && obj.now < 4) {
            $(".flow").children("div").removeClass("active");
            $("#retrieve-form").children("div").hide();
            $("#flow-option").children("button").hide();
            switch (obj.now) {
                case 1:
                    $("#flow-1").addClass("active");
                    $("#form-1").show();
                    $("#next").show();
                    break;
                case 2:
                    $("#flow-1").addClass("active");
                    $("#flow-2").addClass("active");
                    $("#form-2").show();
                    $("#previous").show();
                    $("#next").show();
                    break;
                case 3:
                    $("#flow-1").addClass("active");
                    $("#flow-2").addClass("active");
                    $("#flow-3").addClass("active");
                    $("#form-3").show();
                    $("#sure").show();
                    break;
                default:
                //console.log("steps error!");
            }
        }

    };
    //上一步
    obj.previous = function () {
        obj.now = obj.now - 1;
        obj.change();
    };
    //下一步
    obj.next = function () {
        //判断当前流程表单步骤是否验证通过
        var flag = true;
        if (obj.now == 2) {
            $("#form-" + obj.now + " div:visible").find(":input").each(function () {
                //if(typeof $(this).attr("valid") == "undefined"){
                $(this).trigger("blur");
                //}
                if ($(this).attr("valid") == "true") {
                    flag = flag && true;
                } else {
                    flag = flag && false;
                }
            });
            if (flag) {

                $("#form-msg").html("表单处理中…");

                var url = $("#retrieve-form").attr("data-href");
                var param = {
                    "loginId": $("#loginId").val(),
                    "asspwd": MD5($("#asspwd").val())
                };

                $.ajax({
                    type: "POST",
                    async: false,
                    dataType: "json",
                    url: url,
                    data: param,
                    success: function (json) {
                        if (json.success == true) {
                            obj.now = obj.now + 1;
                            obj.change();
                            $("#form-msg").html("");
                        } else {
                            $("#form-msg").html(json.message);
                        }
                    }
                });
            }
        } else {
            $("#form-" + obj.now + " :input").each(function () {
                //if(typeof $(this).attr("valid") == "undefined"){
                $(this).trigger("blur");
                //}
                if ($(this).attr("valid") == "true") {
                    flag = flag && true;
                } else {
                    flag = flag && false;
                }
            });
            if (flag) {
                obj.now = obj.now + 1;
                obj.change();
            }
        }

    };
    //注册成功
    obj.sure = function () {
        window.location.href = $("#sure").attr("data-href");
    };

    return obj;
})();

//手机验证码验证倒计时
window.countdown = (function () {
    var obj = {};
    obj.number = 60;//倒计时数字
    //发送验证码启动有效倒计时，禁用点击操作
    obj.start = function () {
        $("#regEmail").trigger("blur");//触发手机码验证
        if ($("#regEmail").attr("valid") == "true") {
            var regEmail = $("#regEmail").val();
            $("#send-code").off("click");
            var url = $("#send-code").attr("data-url");
            $.post(url, {"regEmail": regEmail, "smsType": 3}, function (json) {
                if (json.success) {
                    $("#emailCode").val("");
                    window.validate.resetCSS("#emailCode");
                    $("#sms-code-msg").html("<img src='img/success.png'>验证码已发送至您的邮箱，请查收");
                    $("#send-code").css({"background-color": "#cccccc"});
                    obj.interval = setInterval(function () {
                        $("#send-code").html("重发验证码 " + obj.number-- + "s");
                        if (obj.number == 0) {
                            obj.reset();
                        }
                    }, 1000);
                } else {
                    $("#sms-code-msg").html(json.message);
                    setTimeout("$('#sms-code-msg').empty();", 5000);
                    $("#send-code").on("click", function () {
                        window.countdown.start();
                    });
                }
            }, "json");
        }
    };
    //重置状态
    obj.reset = function () {
        obj.number = 60;//倒计时数字
        if (typeof obj.interval != "undefined") {
            clearInterval(obj.interval);
        }
        $("#send-code").html("获取验证码 ");
        $("#send-code").css({"background-color": "#5cb85c"});
        $("#sms-code-msg").empty();
        $("#send-code").on("click", function () {
            window.countdown.start();
        });
    };

    return obj;
})();


//自定义表单验证操作对象
window.validate = (function () {
    var obj = {};
    //o为表单项，option为验证类型，data为验证相关参数
    obj.valid = function (o, option, data) {
        var flag = false;//是否有错
        var msg = "";//错误消息

        $(option).each(function (i) {
            switch (option[i]) {
                case "require":
                    var reg = /(^\s+)|(\s+$)/g;
                    var value = $(o).val();
                    if (value.replace(/^\s+/, '').replace(/\s+$/, '') == "") {
                        msg += "该项为必填项，请输入";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    } else if (reg.test(value)) {
                        msg += "输入的内容前后不允许有空格";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    break;
                case "loginId":
                    var value = $(o).val();
                    //var reg = /^[a-zA-z]\w{3,15}$/;
                    //if (!reg.test(value)) {
                    //    msg += "字母开头数字下划线组成，4-16位";
                    //    flag = true;
                    //    obj.errorCSS(o, msg);
                    //    $(o).attr("valid", false);//验证状态是否为有效
                    //}
                    //远程验证
                    if (flag == false && typeof data[i].url != "undefined") {
                        var url = data[i].url;

                        $.ajax({
                            url: url,
                            async: false,
                            data: {"loginId": value},
                            dataType: 'json',
                            success: function (json) {
                                if (json.success == false) {
                                    flag = true;
                                    msg += json.message;
                                    obj.errorCSS(o, msg);
                                    $(o).attr("valid", false);//验证状态是否为有效
                                }
                            }
                        });
                    }
                    break;
                case "regEmail":
                    var value = $(o).val();
                    var reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
                    if (!reg.test(value)) {
                        msg += "请输入有效的邮箱";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    //远程验证
                    if (flag == false && typeof data[i].url != "undefined") {
                        var url = data[i].url;
                        var loginId = $("#" + data[i].id).val();
                        var param = {
                            "regEmail": value,
                            "loginId": loginId
                        };
                        $.ajax({
                            type: "POST",
                            async: false,
                            dataType: "json",
                            url: url,
                            data: param,
                            success: function (json) {
                                if (json.success == false) {
                                    flag = true;
                                    msg += json.message;
                                    obj.errorCSS(o, msg);
                                    $(o).attr("valid", false);
                                }
                            }
                        });
                    }
                    break;
                case "emailCode":
                    var value = $(o).val();
                    var reg = /^[0-9]{6}$/;
                    if (!reg.test(value)) {
                        msg += "请输入有效的6位数字短信密码";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    //调用远程后端验证有效性
                    if (flag == false) {
                        var url = data[i].url;
                        var regEmail = $("#" + data[i].id).val();

                        $.ajax({
                            url: url,
                            async: false,
                            data: {"regEmail": regEmail, "emailCode": value},
                            dataType: 'json',
                            success: function (json) {
                                if (json.success == false) {
                                    flag = true;
                                    msg += json.message;
                                    obj.errorCSS(o, msg);
                                    $(o).attr("valid", false);//验证状态是否为有效
                                }
                            }
                        });
                    }
                    break;
                case "passwd":
                    var passwd = $(o).val();
                    var re = new RegExp("[a-zA-Z]");
                    var len = re.test(passwd);
                    re = new RegExp("[0-9]");
                    len = re.test(passwd);
                    re = new RegExp("((?=[\x21-\x7e]+)[^A-Za-z0-9])");
                    len = re.test(passwd);
                    if (!len || (passwd.length < 8) || (passwd.length > 20)) {
                        msg += "密码必须包含字母数字符号，8-20位";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    break;
                case "repasswd":
                    var first = $("#" + data[i].id).val();
                    var second = $(o).val();
                    if (first != second) {
                        msg += "输入的密码不一致，请重新输入";
                        flag = true;
                        obj.errorCSS(o, msg);
                        $(o).attr("valid", false);//验证状态是否为有效
                    }
                    break;
                default:
                //console.log("未知参数！");
            }

            if (flag) {//有错直接跳出循环
                return false;
            }
        });

        if (!flag) {//没错时设置验证状态为有效
            obj.resetCSS(o);
            obj.successCSS(o);
            $(o).attr("valid", true);
        }
    };
    obj.successCSS = function (e, msg) {
        if (typeof msg == "undefined") {
            $(e).parent().next("div").html("<img src='img/right.png'>");
        } else {
            $(e).parent().next("div").html("<img src='img/right.png'>" + msg);
        }
    };
    obj.errorCSS = function (e, msg) {
        $(e).parent().next("div").html("<img src='img/wrong.png'>" + msg);
    };

    obj.resetCSS = function (e) {
        $(e).parent().next("div").empty();
    };

    return obj;
})();

//初始化DOM操作绑定事件
$(document).ready(function () {

    $("#btn-protocol").on("click", function () {
        $("#protocol").hide();
        $("#register-flow").show();
    });

    $("#send-code").on("click", function () {
        window.countdown.start();
    });

    $("#previous").on("click", function () {
        window.retrieve.previous();
    });

    $("#next").on("click", function () {
        window.retrieve.next();
    });

    $("#sure").on("click", function () {
        window.retrieve.sure();
    });

});